package com.cloudsigns.factorymanager2.engineering.model;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by tony on 2018/4/17.
 */

public class NormalDailyFeedBackForInstallModel implements INormalDailyFeedBackForInstallModel {

    @Override
    public void submitNormalFeedBackForInstall(final String id, final List<String> imgUrlList, final String itemId, final String status, final CommonEnum.EngineeringORderCategory type, final String content, final String feedbackContentRemark, final CommonEnum.NormalSubType feedbackSubType, final OnNormalFeedBackFroInstallListener onNormalFeedBackFroInstallListener) {

        new Thread(new Runnable() {
            public void run() {
                HttpClient client = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(ProjectUtil.NORMAL_FEEDBACK_FOR_INSTALL);

                HttpResponse response;
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("id", id);

                    JSONArray imgUrlListJson = new JSONArray();
                    if (null != imgUrlList) {
                        for (String url : imgUrlList) {
                            imgUrlListJson.put(url);
                        }
                    }
                    jsonObject.put("imgUrlList", imgUrlListJson);
                    jsonObject.put("itemId", itemId);
                    jsonObject.put("status", status);
                    if (null != type) {
                        if (type == CommonEnum.EngineeringORderCategory.INSTALL)
                            jsonObject.put("type", "INSTALL");
                        if (type == CommonEnum.EngineeringORderCategory.BATCH_INSTALL)
                            jsonObject.put("type", "BATCH_INSTALL");
                        if (type == CommonEnum.EngineeringORderCategory.CONSTRUCT)
                            jsonObject.put("type", "CONSTRUCT");
                        if (type == CommonEnum.EngineeringORderCategory.MAINTAIN)
                            jsonObject.put("type", "MAINTAIN");
                        if (type == CommonEnum.EngineeringORderCategory.PUBLICATION)
                            jsonObject.put("type", "PUBLICATION");
                        if (type == CommonEnum.EngineeringORderCategory.PATROL)
                            jsonObject.put("type", "PATROL");
                    }

                    jsonObject.put("content", content);
                    jsonObject.put("feedbackContentRemark", feedbackContentRemark);
                    if (null != feedbackSubType) {
                        if (feedbackSubType == CommonEnum.NormalSubType.CONSTRUCT) {
                            jsonObject.put("feedbackSubType", "CONSTRUCT");

                        } else if (feedbackSubType == CommonEnum.NormalSubType.LOGISTICS) {
                            jsonObject.put("feedbackSubType", "LOGISTICS");
                        }
                    }


                    StringEntity se = new StringEntity(jsonObject.toString(), HTTP.UTF_8);

                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpPost.setEntity(se);

                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != onNormalFeedBackFroInstallListener) {

                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            onNormalFeedBackFroInstallListener.submitNormalFeedBackForInstallSunccessed();
                        } else {
                            String desc = code;
                            if (!json.isNull("desc")) {
                                desc += "," + json.getString("desc");
                            }
                            onNormalFeedBackFroInstallListener.submitNormalFeedBackForInstallFailed(desc);
//                            String error_str = code;
//                            error_str += "," + ConstantUtil.getErrorCode(code);
//                            if (!json.isNull("error_str")) {
//                                error_str += "," + json.getString("error_str");
//                            }
//
//                            onNormalFeedBackFroInstallListener.submitNormalFeedBackForInstallFailed(error_str);


                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }

            }
        }).start();
    }


    public interface OnNormalFeedBackFroInstallListener {
        void submitNormalFeedBackForInstallSunccessed();

        void submitNormalFeedBackForInstallFailed(String desc);
    }
}
